<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <title>Document</title>
    <link rel="stylesheet" href="./cart.css" />
</head>

<body>
    <div id="app">
        <div class="container">
            <my-cart></my-cart>
        </div>
    </div>
    <script type="text/javascript" src="./vue.js"></script>
    <script type="text/javascript">
        var CartTitle = {
            props: ['uname'],
            template: `
        <div class="title">{{uname}}的商品</div>
      `,
        };
        var CartList = {
            props: ['list'],
            template: `
              <div>
                <div class="item" v-for='item in list' :key='item.id'>
                  <img :src="item.img"/>
                  <div class="name">{{item.name}}</div>
                  <div class="change">
                    <a href="" @click.prevent="sub(item.id)">－</a>
                    <input type="text" class="num" :value="item.num" @blur="changeNum(item.id,$event)"/>
                    <a href="" @click.prevent="add(item.id)">＋</a>
                  </div>
                  <div class="del" @click='del(item.id)'>×</div>
                </div>
              </div>
            `,
            methods: {
                del: function(id) {
                    // console.log(this);
                    this.$emit('del-data', id);
                },
                changeNum: function(id, event) {
                    // console.log(id, event.target.value);
                    this.$emit('change-num', {
                        id: id,
                        type: 'change',
                        num: event.target.value,
                    });
                },
                add: function(id) {
                    this.$emit('change-num', {
                        id: id,
                        type: 'add',
                    });
                },
                sub: function(id) {
                    this.$emit('change-num', {
                        id: id,
                        type: 'sub',
                    });
                },
            },
        };
        var CartTotal = {
            props: ['list'],
            template: `
                <div class="total">
                    <span>总价：￥{{total}}</span>
                    <button>结算</button>
                </div>
            `,
            computed: {
                total: function() {
                    // 计算商品的总价
                    var t = 0;
                    this.list.forEach((item) => {
                        t += item.price * item.num;
                    });
                    return t;
                },
            },
        };
        Vue.component('my-cart', {
            data: function() {
                return {
                    uname: '张三',
                    list: [{
                        id: 1,
                        name: 'TCL彩电',
                        price: 1000,
                        num: 1,
                        img: 'img/a.jpg',
                    }, {
                        id: 2,
                        name: '机顶盒',
                        price: 1000,
                        num: 1,
                        img: 'img/b.jpg',
                    }, {
                        id: 3,
                        name: '海尔冰箱',
                        price: 1000,
                        num: 1,
                        img: 'img/c.jpg',
                    }, {
                        id: 4,
                        name: '小米手机',
                        price: 1000,
                        num: 1,
                        img: 'img/d.jpg',
                    }, {
                        id: 5,
                        name: 'PPTV电视',
                        price: 1000,
                        num: 2,
                        img: 'img/e.jpg',
                    }, ],
                };
            },
            methods: {
                delData: function(id) {
                    // console.log(id);
                    var index = this.list.filter(function(item) {
                        return item.id == id;
                    });
                    this.list.splice(index, 1);
                },
                changeNum: function(val) {
                    console.log(val);
                    if (val.type == 'change') {
                        this.list.some((item) => {
                            if (item.id == val.id) {
                                item.num = val.num;
                                return true;
                            }
                        });
                    } else if (val.type == 'sub') {
                        this.list.some((item) => {
                            if (item.id == val.id) {
                                item.num -= 1;
                                return true;
                            }
                        });
                    } else if (val.type == 'add') {
                        this.list.some((item) => {
                            if (item.id == val.id) {
                                item.num += 1;
                                return true;
                            }
                        });
                    }
                },
            },
            template: `
                <div class='cart'>
                  <cart-title :uname='uname'></cart-title>
                  <cart-list :list='list' @del-data='delData($event)' @change-num='changeNum($event)'></cart-list>
                  <cart-total :list='list'></cart-total>
                </div>
              `,
            components: {
                'cart-title': CartTitle,
                'cart-list': CartList,
                'cart-total': CartTotal,
            },
        });
        var vm = new Vue({
            el: '#app',
            data: {},
        });
    </script>
</body>

</html>